import pandas as pd
import numpy as np
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
import pickle
import matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler


def save(obj, name):
    path = f'./processed_data/{name}.pkl'
    with open(path, 'wb') as f:
        pickle.dump(obj, f)


def proc_data():
    path = r'F:\DataCenter\data_collections\races\flink_cup_rc/'
    # data = pd.read_csv(path + 'train.csv', header=None, names='uuid visit_time user_id item_id features label'.split())

    # data.to_pickle('./processed_data/train.pkl')

    data = pd.read_pickle('./processed_data/train.pkl')

    print(data.shape)
    print(data.head())
    print(data['user_id'].nunique())
    print(data['item_id'].nunique())

    r = data['features'][0]
    print(len(r.split()))

    feats = data['features'].str.split().tolist()
    y = data['label'].values

    col_num = len(feats[0])
    feats = pd.DataFrame(feats, columns=[f"f{i}" for i in range(col_num)])

    columns = feats.columns
    for col in columns:
        feats[col] = feats[col].astype(float)

    save(feats, "df_features")


def load(name):
    file = f'./processed_data/{name}.pkl'
    with open(file, 'rb') as f:
        data = pickle.load(f)
    return data


def ext_features():
    min_max = MinMaxScaler()

    feats = load('df_features')
    print(feats.describe())

    feats_min_max = min_max.fit_transform(feats)
    df = pd.DataFrame(feats_min_max, columns=feats.columns)
    save(df, "feats_min_max")
    # return feats_min_max

    # cols = feats.columns
    # for col in cols:
        # feat = feats[col].sample(10000)
        # plt.hist(feat)
        # plt.show()


def analyze():
    data = pd.read_pickle('./processed_data/train.pkl')
    bad_user = data['user_id'][data['label'] == 1].nunique()
    all_user = data['user_id'].nunique()
    print(bad_user)
    print(all_user)
    print("比率:", bad_user/all_user)


def train_lr():
    feats = load('feats_min_max')
    data = pd.read_pickle('./processed_data/train.pkl')
    X = feats.values
    y = data['label'].values
    print(X[0])
    print(X.shape)

    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1)

    lr = LogisticRegression()
    lr.fit(X_train, y_train)
    preds = lr.predict(X_test)

    r = classification_report(y_test, preds)
    print(r)


if __name__ == "__main__":
    proc_data()
    # ext_features()
    # train_lr()
    # analyze()

